package org.jplearning.io;

import java.io.File;
import java.io.OutputStreamWriter;
import java.net.ContentHandler;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;


public class DictionarySAXReader extends DefaultHandler{
	// TAG DEFINITION
	private static final String title = "title";
	private static final String description = "description";
	private static final String created_by = "created-by";
	private static final String modified_by = "modified-by";
	private static final String modification_date = "modification-date";
	private static final String tags = "tags";
	private static final String tag = "tag";
	private static final String entries = "entries";
	private static final String entry = "entry";
	private static final String word = "word";
	private static final String meaning = "meaning";
	private static final String translation = "translation";
	private static final String definition = "definition";
	private static final String example = "example";
	private static final String phrase = "phrase";
	
	private DictionaryBuilder builder;
	private String curVal;
	
	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyy");
	
	@Override
	public void startDocument() throws SAXException {
		System.out.println("START DOCUMENT");
		builder = new DictionaryBuilder();
	}
	
	@Override
	public void endDocument() throws SAXException {
		System.out.println("END DOCUMENT");
	}
	
	@Override
	public void startElement(String uri, String sName, String qName, Attributes attributes) throws SAXException {
		System.out.println("START: " + qName);
		
		if(tags.equals(qName)){
			
		}
		else if(tag.equals(qName)){
			
		}
		else if(entries.equals(qName)){
			
		}
		else if(entry.equals(qName)){
			String id = attributes.getValue("id");
			builder.startEntry(id);
		}
		else if(word.equals(qName)){
			String alphabet = attributes.getValue("alphabet");
			builder.startWord(alphabet);
		}
		else if(meaning.equals(qName)){
			builder.startMeaning();
		}
		else if(translation.equals(qName)){
			
		}
		else if(definition.equals(qName)){

		}
		else if(example.equals(qName)){
			builder.startExample();
		}
		else if(phrase.equals(qName)){
			
		}
	}
	
	@Override
	public void endElement(String uri, String sName, String qName) throws SAXException {
		try {
			System.out.println("END: " + qName);
			
			if(title.equals(qName)){
				builder.getDictionary().setTitle(curVal);
			}
			else if(description.equals(qName)){
				builder.getDictionary().setDescription(curVal);
			}
			else if(created_by.equals(qName)){
				builder.getDictionary().setCreated_by(curVal);
			}
			else if(modified_by.equals(qName)){
				builder.getDictionary().setModified_by(curVal);
			}
			else if(modification_date.equals(qName)){
				Date date = DATE_FORMAT.parse(curVal);
				builder.getDictionary().setModification_date(date);
			}
			else if(created_by.equals(qName)){
				builder.getDictionary().setCreated_by(curVal);
			}
			else if(tags.equals(qName)){
				
			}
			else if(tag.equals(qName)){
				
			}
			else if(entries.equals(qName)){
				
			}
			else if(entry.equals(qName)){
				
			}
			else if(word.equals(qName)){
				
			}
			else if(meaning.equals(qName)){
				
			}
			else if(translation.equals(qName)){
				
			}
			else if(definition.equals(qName)){
				
			}
			else if(example.equals(qName)){
				
			}
			else if(phrase.equals(qName)){
				
			}
		} catch (ParseException e) {
			throw new SAXException(e);
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		curVal = new String(ch, start, length).trim();
	}
	
	public static void main(String[] args) {
		try{
			SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
			DictionarySAXReader handler = new DictionarySAXReader();

			parser.parse(new File("conf/dictionary.xml"), handler);
			
		}
		catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
